package com.monkeysonnet.b2dFluent;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.ChainShape;

public class ChainFactory extends FixtureFactory<ChainFactory>
{
	private ChainShape _shape;
	
	public ChainFactory()
	{
		this(false);
	}
	
	ChainFactory(boolean managed)
	{
		super(managed);
		_shape = new ChainShape();
		_def.shape = _shape;
	}
	
	public ChainFactory asLine(Vector2[] verts)
	{
		_shape.createChain(verts);
		return this;
	}
	
	public ChainFactory asLoop(Vector2[] verts)
	{
		_shape.createLoop(verts);
		return this;
	}
	
	public ChainFactory nextVertex(Vector2 v)
	{
		_shape.setNextVertex(v);
		return this;
	}
	
	public ChainFactory prevVertex(Vector2 v)
	{
		_shape.setPrevVertex(v);
		return this;
	}
	
	public ChainFactory nextVertex(float x, float y)
	{
		_shape.setNextVertex(x, y);
		return this;
	}
	
	public ChainFactory prevVertex(float x, float y)
	{	
		_shape.setPrevVertex(x, y);
		return this;
	}
	
	@Override
	void clear()
	{
		super.clear();
		_shape.dispose(); // trying to reuse chain shape causes assertion failure in b2ChainShape.cpp
		_shape = new ChainShape();
		_def.shape = _shape;
	}

	@Override
	public void free()
	{
		B2d.free(this);
	}
}
